package com.lp.batch.domain;

import java.util.ArrayList;
import java.util.List;

/**
 * Represents an element in the taxonomy XML file. There are 2 type of elements
 * in a taxonomy file: Taxonomy and Node.
 * 
 * @author Zainul Franciscus
 * 
 */
public class Element {

	/**
	 * a node in the taxonomy
	 */
	protected List<Node> childNodes = new ArrayList<Node>();

	/**
	 * @return the first child in this taxonomy
	 */
	public Node getFirstChild() {
		if (childNodes.size() == 0) {
			return null;
		} else {
			return childNodes.get(0);
		}
	}

	/**
	 * @return the node
	 */
	public List<Node> getChildNodes() {
		return childNodes;
	}

	/**
	 * @param node
	 *            to be added into this element
	 */
	public void addChildNode(Node node) {
		this.childNodes.add(node);
	}

	/**
	 * @param node
	 *            the node to set
	 */
	public void setChildNodes(List<Node> node) {
		this.childNodes = node;
	}

	/**
	 * @return every node in this taxonomy
	 */
	public List<Node> getDescendants() {

		List<Node> allNodes = new ArrayList<Node>();

		for (Node node : childNodes) {
			allNodes.add(node);
			allNodes.addAll(node.getDescendants());
		}

		return allNodes;
	}

	/**
	 * @return the number of child that this node has
	 */
	public int countChildNodes() {
		return childNodes.size();
	}

}